<?PHP
/**
 * \file    validatequestions.php
 * \author  Manori
 * \date    May 10, 2010
 * \version 1.0
 */
?>
<?PHP
session_start();
require('../basicInfo/user.php');
require('question.php');
$_SESSION['currentPage'] = "questionbank";
if(isset($_SESSION['userinfo'])) {    
$userinfo=unserialize($_SESSION['userinfo']);
}else {
  header("Location:../../index.php");
}
$vid1=$userinfo->getUid();
?>
<?PHP

$pendingQuestionArr=QuestionController::getPendingQuestions();
$rowsPerPage = 4;
$pendingQuestionArr[sizeof($pendingQuestionArr)]=null;
$numOfPages = (int) ((sizeof($pendingQuestionArr)-1) / $rowsPerPage);
if ((sizeof($pendingQuestionArr)-1) % $rowsPerPage != 0) {
    $numOfPages++;
}

if ($_GET['page'] == "") {
    $_GET['page'] = 1;
}


function approveQuestionByValidator($qid,$uid,$gold){
	$ts=date("d/m/y : H:i:s", time()) ;
	global $vid1;
	
	$result = QuestionController::approveQuestionV($vid1,$ts,$qid,$uid,$gold);
	
	if(isset($_SESSION['userinfo'])) {    
		$userinfo=unserialize($_SESSION['userinfo']);
		$userinfo->setGold($userinfo->getGold()+100);
	}
	
	

	if($result){
		echo '<script language="javascript">alert("Question successfully approved");'
		,'location.replace("validatequestions.php?page");</script>';
	} else {
		echo '<script language="javascript">alert("Question approval not successful");'
		,'location.replace("validatequestions.php?page");</script>';
	}
}

function rejectQuestionByValidator($qid){
	$ts=date("d/m/y : H:i:s", time()) ;
	$result = QuestionController::rejectQuestionV($vid1,$ts,$qid);
	
	
	if($result){
		echo '<script language="javascript">'
		,'location.replace("validatequestions.php?page");'
		,'alert("Question successfully rejected");</script>';
	} else {
		echo '<script language="javascript">alert("Question rejection not successful");'
		,'location.replace("validatequestions.php?page");</script>';
	}
}

if (isset($_POST['approve'])){
	$qid=$_POST['qid'];
	$uid=$_POST['uid'];
	$gold=100;
	
    approveQuestionByValidator($qid,$uid,$gold);
}
 
if (isset($_POST['reject'])){
	$qid=$_POST['qid'];
    rejectQuestionByValidator($qid);
}

?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>Debugger MMO</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="en-US" />
<script type="text/javascript">
            function changePage(linkNum) {
                location.replace("validatequestions.php?page="+linkNum);
            }

</script>
<style type="text/css"><!--
    #question_view
    {
        float:left;
        width: 496px;
        height:320px;
        border:1px solid #639684;
        border-top-style:4px solid #D2B48C;
        margin-top:8px;
        margin-bottom:20px;
        margin-left:2px;
        margin-right:8px;
        background-color: #FFEBCD;

    }

    #view_heading
    {
        width: 496px;
        height:20px;
        margin-top:0px;
        background-color:#DEB887;
        font-family:Verdana;
        text-align:left;
        color: white;
        font-size:10px;
    }

    
    #question_view table 
    {
        border:1px solid #DEB887;
        padding: 4px;
        margin-left:5px;
		margin-top:10px;
		margin-right:5px;
		margin-bottom:auto;
    }

    #question_view table tr
    {
        border:1px solid #DEB887;
		font-weight:normal;
    }

	#question_view table tbody
    {
        font-weight:normal;
    }

    #edit_text
    {
        color:gray;
        text-decoration:underline;
    }
    #question_text
    {
        color:blue;
        text-decoration:underline;
    }
    #rejected_text
    {
        color:red;
    }

    #approvebutton
    {
		color: #1B6F1B;
        background-color:#FFDB4D;
        float: left;
		padding:5px 5px 5px 5px;
        cursor: pointer;
		font-family: Verdana;
		font-size: 10px;
		font-weight:bold;
    }

	#rejectbutton
    {
		color: #FF0000;
        background-color:#FFDB4D;
        float: left;
		padding:5px 5px 5px 5px;        
		cursor: pointer;
		font-family: Verdana;
		font-size: 10px;
		font-weight:bold;
    }

	#qlink a:link
	{
		
		text-decoration:underline;
	}

    #page_numbers
    {
       background-color: #FFEBCD;
       float:right;
       border:none;
	   cursor:pointer;
       margin-right:100px;
       margin-bottom:10px;
       margin-top:0px;
    }

    
    
    --></style>
	<style type="text/css"><!--
            li.pageLink {
                cursor: pointer;
            }
            div#pageLinks a#selected {
                background-color: #CCCC99;
            /*  border: #886136 1px solid;    */
            }
            #pageLinks ul
            {
                margin: 0;
                padding: 0;
                list-style-type: none;
                text-align: center;
            }
            #pageLinks ul li {
                display: inline;
            }
            #pageLinks ul li a
            {
                text-decoration: none;
                padding: .2em 1em;
                color: #000000;
            }
            #pageLinks ul li a:hover
            {
                color: #c0c0c0;
            }
        --></style>


	
</head>


<body>
<?php include "../../layout/header.php"; ?>
<?php include "../../layout/navleft.php"; ?>
<div id="question_view">
    <div id="view_heading">Welcome Validator! -Pending Questions</div>    
    <table cellspacing="10px" >
        <thead>
            <tr>
                <th>Question</th>
                <th>Level</th>
				<th>Type</th>
                <th>Time limit</th>
				<th>Date</th>
                <th>  Action</th>
            </tr>
        </thead>
        <tbody>
             <?PHP
               if ($pendingQuestionArr[0] == null) {
					echo "<tr><td>Currently no pending questions</td></tr>";
               } else {
				   $page = ($_GET['page'] - 1) * $rowsPerPage;
                   $endPage = $page + $rowsPerPage;
                   for ( ; $page < $endPage; $page++) {
						if ($pendingQuestionArr[$page] == null) {
								break;
                        }
                        echo "<tr>";
                        if ($pendingQuestionArr[$page] != null) {
   echo '<td><a href="questionfb.php?page&qid='.$pendingQuestionArr[$page]->getQuestId().'">'.
                                     $pendingQuestionArr[$page]->getQuestion().'</a></td><td>'.
                                     $pendingQuestionArr[$page]->getLevel().'</td><td>'.
									 $pendingQuestionArr[$page]->getQType().'</td><td>'.
									 $pendingQuestionArr[$page]->getTime().'</td><td>'.
									 $pendingQuestionArr[$page]->getQDate().'</td>';
									 echo '<td>';
									 echo '<form action="" method="POST">'
				,'<input type="hidden" name="qid" value="'.$pendingQuestionArr[$page]->getQuestId().'"/>'
				,'<input type="hidden" name="uid" value="'.$pendingQuestionArr[$page]->getUserId().'"/>'
				,'<input type="hidden" name="page"/>'
					                    ,'<input type="submit" id="approvebutton" name="approve" value="Approve"/>'
					                       ,'</form>';
									 echo '</td><td>';
									 echo '<form action="" method="POST">'
			,'<input type="hidden" name="qid" value="'.$pendingQuestionArr[$page]->getQuestId().'"/>'
					                    ,'<input type="submit" id="rejectbutton" name="reject" value="Reject"/>'
					                       ,'</form>';
									 echo '</td>';
                                     #echo '<input type="button" id="approvebutton" value="Approve" name="approve" #onclick="approveQuestion()"/>';
									 
									 #echo '</td>';
									 #echo '<td>';
                                     #echo '<input type="button" id="rejectbutton" value="Reject" name="reject" #onClick="rejectQuestion('.$pendingQuestionArr[$page]->getQuestion().')"/>';
									 #echo '</td>';
						}
                         echo "</tr>";
				   }
			   }
			 ?>                                   
        </tbody>
     </table>
     <div id="pageLinks">
			<ul>
			     <?PHP
                  for ($i = 0; $i < $numOfPages; $i++) {
						echo "<li class='pageLink' onclick=changePage(".($i+1).")><a";
                        if ($i == $_GET['page'] - 1) {
								echo " id='selected'";
                        }
                        echo ">".($i+1)."</a></li>";
                  }
				  ?>
             </ul>
	 </div>
</div>
<?php include "../../layout/navright.php"; ?>
<?php include "../../layout/footer.php"; ?>
</body>
</html>    